local DrawControl = require "draw_control"

local MainWindow= class(zui.Window)

function MainWindow:ctor()
	self:createWindow("test",self.POS_CENTER,self.POS_CENTER,800,600)
end


function MainWindow:onInit()
	log("onInit window")
	self:loadLayout("layout/ui.xml")
	self:initFrdList()
end


local frdItemXml=[[
<xml>
<ListCell reuseid="0" type="hor">
	<ImageControl asyncfile="image/li.jpg" imagesize="40,40" mask="image/head_mask.png" width="40dp" height="40dp" padding="10dp,10dp,0,0"/>
	<VLayout inset="10dp,10dp,10dp,10dp" childpadding="3dp">
		<HLayout height="20dp">
			<Label text="测试群" fontsize="14dp"/>
			<Label text="10:12" textcolor="gray" autowidth="true" fontsize="10dp" textpadding="0,2dp,0,0"/>
		</HLayout>
		<Label text="王某某:测试消息" textcolor="gray" fontsize="12dp"/>
	</VLayout>
</ListCell>
</xml>
]]

function MainWindow:initFrdList()
	self._frdList = self:findControl("frdList")
	self._frdList:setDataSource({
		numberOfIndex=MainWindow.getFrdListCount,
		heightForIndex=MainWindow.getFrdItemHeight,
		cellAtIndex=MainWindow.frdCellAtIndex,
	})
	self._frdBuilder = zui.Builder.new()
	self._frdBuilder:loadXmlString(frdItemXml)

	self._frdList:reload()
end

function MainWindow:getFrdListCount()
	return 100
end

function MainWindow:getFrdItemHeight()
	return 60*self:getScale()
end

function MainWindow:frdCellAtIndex(size,idx)

	local item = self._frdList:getReusableCell(0)
	if not item then
		item = self._frdBuilder:create(self)
	else

	end
	
	return item
end

function MainWindow:onLayout()
	log("onLayout window")
end

--窗口尺寸变化
function MainWindow:onMaximized()
	self:findControl("maxbtn"):setVisible(false)
	self:findControl("restorebtn"):setVisible(true)
end

function MainWindow:onRestored()
	self:findControl("maxbtn"):setVisible(true)
	self:findControl("restorebtn"):setVisible(false)	
end

--点击任务栏关闭
function MainWindow:onCommandClose()
	self:quitApp()
end

function MainWindow:showMax()
	self:setMaximize()
end

function MainWindow:showRestore()
	self:setRestore()
end

function MainWindow:showMin()
	self:setMinimize()
end

function MainWindow:startAnim(btn)
	log("播放动画")
	self._reverse=false
	self:findControl("animCtl"):startAnimation()
end

function MainWindow:onStateChange(animCtl,state)
	if state==animCtl.STATE_START then
		log("开始播放动画")
	else
		log("播放动画结束")
		--self._reverse = not self._reverse
		--animCtl:setReverse(self._reverse)
		--self:findControl("animCtl"):startAnimation()
	end
end


function MainWindow:quitApp()
	zui.Application.instance():quit()
	--self:close()
	print("close window")
end

function MainWindow:luaGc()
	collectgarbage("collect")
end

function MainWindow:dragControl(btn)
	btn:beginDrag()
end

function MainWindow:onDrop(receiver,control)
	log("onDrop:"..control:getClass())
end

function MainWindow:selectChat(opt,bsel)
	if bsel then
		self:findControl("switch"):selectItem(0)
	end
end

function MainWindow:selectDemo(opt,bsel)
	if bsel then
		self:findControl("switch"):selectItem(1)
	end
end

function MainWindow:sendMsg(btn)
	local edit = self:findControl("msgEdit")
	local text = edit:getText()
	edit:setText("")
	if #text>0 then
		self:findControl("list"):sendMsg(text)
	end
end

function MainWindow:showSetting()
	self._remocePopup=false
	self:findControl("root"):addFromXml("layout/popup.xml")
end

function MainWindow:onAnimFinish(ctrl,state)
	if state == ctrl.STATE_STOP and self._remocePopup then
		self:findControl("root"):removeLast()
	end
end

function MainWindow:closePopup(btn)
	self:findControl("popupAnim"):setReverse(true)
	self:findControl("popupAnim"):startAnimation()
	self._remocePopup=true
end

mainWnd = MainWindow.new()

